﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Kokomo.Mirror.CliMetadata.Tables
{
	abstract class MetadataRowTypeConverter<TRow> : TypeConverter
	{
		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType != typeof(string)) return null;
			if (!(value is TRow)) return null;

			MetadataRowPropertyDescriptor property = context.PropertyDescriptor as MetadataRowPropertyDescriptor;
			if (property == null) return null;

			string str = this.ConvertToString((TRow)value, property.Container);
			return str;
		}

		protected abstract string ConvertToString(TRow row, MetadataContainer container);

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return (destinationType == typeof(string));
		}
	}
}
